package com.binarycube.webserver.wrappers;

import java.io.IOException;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

/**
 * Wrapper for freemarker templates to have limited access to the response object
 * @author chris
 *
 */
public class WrappedResponse extends Wrapper{

	HttpServletResponse theResponse;
	
	public WrappedResponse(HttpServletResponse response) {
		theResponse = response;
	}
	
	/**
	 * Sets the response content type
	 * @param type
	 */
	public void setContentType(String type) {
		theResponse.setContentType(type);
		
	}
	
	public void setHeader(String name, String value) {
		theResponse.setHeader(name, value);
	}
	
	public void setCharacterEncoding(String encode) {
		theResponse.setCharacterEncoding(encode);
	}
	
	
	public void addCookie(String name, String value){
		Cookie cookie = new Cookie(name, value);
		cookie.setPath("/"); //Default to server root
		theResponse.addCookie(cookie);
	}
	
	public void addCookie(String name, String value, String domain, int expiry){
		Cookie cookie = new Cookie(name, value);
		cookie.setDomain(domain);
		cookie.setMaxAge(expiry);
		cookie.setPath("/"); //Default to server root
		theResponse.addCookie(cookie);
	}

	public void redirect(String url) {
		try {
			theResponse.sendRedirect(url);
			theResponse.flushBuffer();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
   }
	
	
	
}
